using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using dender.dvdcover.gui.custom;
using dender.dvdcover.core;
using System.Drawing.Printing;

namespace dender.dvdcover.gui
{
    public partial class mainForm : Form
    {
        Cover mCover;

        public mainForm()
        {
            InitializeComponent();
        }


        private void mainForm_Load(object sender, EventArgs e)
        {
            //Format f = CreateTestCover();

            //Cover c = new Cover(f);
            //this.Cover = c;
        }

        private static Format CreateTestCover()
        {
            Format f = new Format(275, 183);
            Part p = new Part(f, new PointF(0, 0), 275, 183);
            p.Name = "Main";
            f.Parts.Add(p);
            f.PaperKind = PaperKind.A4;
            return f;
        }

        public PartPicturebox SelectedPart
        {
            get
            {
                PartPicturebox selectedPictureBox = null;
                if (mainTabControl.SelectedTab != null)
                {
                    if (mainTabControl.SelectedTab is CoverTabPage)
                    {
                        selectedPictureBox = ((CoverTabPage)mainTabControl.SelectedTab).PartPictureBox;
                    }
                }
                return selectedPictureBox;
            }
        }

        public Cover Cover
        {
            get
            {
                return mCover;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("Cover");

                mCover = value;
                mainTabControl.TabPages.Clear();
                foreach (Part part in value.Parts)
                {
                    mainTabControl.TabPages.Add(new CoverTabPage(part.Name, part, partMenuStrip));
                }
            }
        }

        private string ShowOpenDialog()
        {
            string returnValue = null;
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.CheckFileExists = true;
            openDialog.Multiselect = false;
            openDialog.Filter = "JPEG files|*.jpg|" +
                                "BMP files|*.bmp";

            DialogResult res = openDialog.ShowDialog();
            if (res == DialogResult.OK)
            {
                returnValue = openDialog.FileName;
            }
            return returnValue;
        }


        #region Menu Actions

        private void browseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PartPicturebox selected = this.SelectedPart;
            string filename = ShowOpenDialog();
            if (filename != null)
            {
                Bitmap image = new Bitmap(filename);
                selected.LoadImage(image);
            }
        }      

        private void centeredCropResizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PartPicturebox selected = this.SelectedPart;
            if (selected != null)
            {
                selected.CenterResize();
            }
        }

        private void resetTransformToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PartPicturebox selected = this.SelectedPart;
            if (selected != null)
            {
                selected.ResetTransforms();
            }
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.Cover != null)
            {
                CoverPrintDocument document = this.Cover.GeneratePrintDocument();
                PrintPreviewDialog dialog = new PrintPreviewDialog();
                dialog.Document = document;
                dialog.ShowDialog();
            }
        }

        #endregion Menu Actions

        private void infoGroupBox_Enter(object sender, EventArgs e)
        {

        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            coverForm form = new coverForm();
            form.Owner = this;
            form.ShowDialog();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox about = new AboutBox();
            about.Owner = this;
            about.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }



    }
}